package com.zhiyou100.api.io_demo;


import java.io.*;

/**
 * 转换流：
 *  InputStreamReader：将字节流转换成字符流，转换时可以指定编码格式
 *  OutputStreamWriter：输出转换流    使用时可以指定编码格式
 */
public class Demo4 {

    public static void main(String[] args) throws IOException {
        testWriter();
    }

    //读取file.txt
    public static void testReader() throws IOException {

        File srcFile = new File("./java_api/file.txt");
        //利用转换流
        FileInputStream fis = new FileInputStream(srcFile);
        InputStreamReader isr = new InputStreamReader(fis,"GBK");
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        while (s != null){
            System.out.println(s);
            s = br.readLine();
        }
    }


    public static void testWriter() throws IOException {
        String s = "好好学习，天天向上!!!!!!";
        File targetFile = new File("./java_api/file3.txt");
        //输出转换流
        OutputStream os = new FileOutputStream(targetFile);
        OutputStreamWriter osw = new OutputStreamWriter(os,"GBK");
        BufferedWriter bw = new BufferedWriter(osw);
        bw.write(s);
        bw.flush();
    }



}
